package com.techwells.learningkites.controller;

import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.TextView;

import com.techwells.learningkites.R;
import com.techwells.learningkites.adapter.AvatarGridAdapter;
import com.techwells.learningkites.model.ChooseUserAvatarViewModel;
import com.techwells.learningkites.networkservice.KitesServiceMediator;
import com.techwells.learningkites.networkservice.model.Avatar;
import com.techwells.taco.base.CommonBaseActivity;
import com.techwells.taco.tasktool.TaskToken;

public class ChooseUserAvatar extends CommonBaseActivity implements
		OnItemClickListener {

	private TextView tvMale, tvFemale, tvUnknown;
	private GridView gvMaleAvatar, gvFemaleAvatar, gvUnknownAvatar;
	private ChooseUserAvatarViewModel presentModel;
	private List<Avatar> listAvatar;
	private AvatarGridAdapter maleAdapter, femaleAdapter, unknownAdapter;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_choose_user_avatar);

		initTitleBar(R.string.avatar, defaultLeftClickListener);
		initViews();
	}

	private void initViews() {
		tvMale = (TextView) findViewById(R.id.tv_male);
		gvMaleAvatar = (GridView) findViewById(R.id.gv_male_avatars);
		tvFemale = (TextView) findViewById(R.id.tv_female);
		gvFemaleAvatar = (GridView) findViewById(R.id.gv_female_avatars);
		tvUnknown = (TextView) findViewById(R.id.tv_unknown);
		gvUnknownAvatar = (GridView) findViewById(R.id.gv_unknown_avatars);

	}

	@Override
	public void alreadyBindBaseViewModel() {
		// TODO Auto-generated method stub
		super.alreadyBindBaseViewModel();

		presentModel = (ChooseUserAvatarViewModel) baseViewModel;
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();

		doRequestGetAvatars();
	}

	private void doRequestGetAvatars() {
		doTask(KitesServiceMediator.SERVICE_GET_AVATARS, null);
	}

	@Override
	public void refreshData(TaskToken token) {
		// TODO Auto-generated method stub
		super.refreshData(token);

		String method = token.method;
		if (method.equals(KitesServiceMediator.SERVICE_GET_AVATARS)) {
			listAvatar = presentModel.listAvatar;
			maleAdapter = new AvatarGridAdapter(this, listAvatar.get(0).urls);
			gvMaleAvatar.setAdapter(maleAdapter);
			gvMaleAvatar.setOnItemClickListener(this);

			femaleAdapter = new AvatarGridAdapter(this, listAvatar.get(1).urls);
			gvFemaleAvatar.setAdapter(femaleAdapter);
			gvFemaleAvatar.setOnItemClickListener(this);

			unknownAdapter = new AvatarGridAdapter(this, listAvatar.get(2).urls);
			gvUnknownAvatar.setAdapter(unknownAdapter);
			gvUnknownAvatar.setOnItemClickListener(this);
		}
	}

	@Override
	public void onItemClick(AdapterView<?> adapterView, View view, int position, long arg3) {
		Intent intent = new Intent(this, SettingUserInfoActivity.class);
		switch (adapterView.getId()) {
		case R.id.gv_male_avatars:
			intent.putExtra("avatar", listAvatar.get(0).urls.get(position));
			setResult(1, intent);
			finish();
			break;
		case R.id.gv_female_avatars:
			intent.putExtra("avatar", listAvatar.get(1).urls.get(position));
			setResult(1, intent);
			finish();
			break;
		case R.id.gv_unknown_avatars:
			intent.putExtra("avatar", listAvatar.get(2).urls.get(position));
			setResult(1, intent);
			finish();
			break;

		default:
			break;
		}

	}

}
